set more off
clear all
cls

local save_figures_flag=1


********************************************************************************
***************************EUROSTAT, prices*************************************
********************************************************************************
import delimited "../international_data/original_data/prices/data_eurostat.csv", clear

rename time_period year
rename geo country 

drop dataflow freq unit unit obs_flag
rename obs_value value

sort country coicop year

replace coicop="CPI_all" if coicop=="CP00"
replace coicop="CPI_hh_items" if coicop=="CP05"
replace coicop="CPI_recreation_total" if coicop=="CP09"
replace coicop="CPI_recreation_audio_video" if coicop=="CP091"
replace coicop="CPI_recreation_books" if coicop=="CP095"
replace coicop="CPI_recreation_other_dur" if coicop=="CP092"
replace coicop="CPI_recreation_other_goods" if coicop=="CP093"
replace coicop="CPI_recreation_pack_hol" if coicop=="CP096"
replace coicop="CPI_recreation_services" if coicop=="CP094"

reshape wide value, i(country year) j(coicop) string

local list total audio_video books other_dur other_goods pack_hol services

foreach var of local list {
	rename valueCPI_recreation_`var' CPI_recreation_`var' 
}
rename valueCPI_all CPI_all
rename valueCPI_hh_items CPI_hh_items



rename country country_full
gen country=""

replace country="AUT" if country_full=="AT"
replace country="BEL" if country_full=="BE"
replace country="BGR" if country_full=="BG"
replace country="HRV" if country_full=="HR"
replace country="CYP" if country_full=="CY"
replace country="CZE" if country_full=="CZ"
replace country="DEU" if country_full=="DE"
replace country="DNK" if country_full=="DK"
replace country="EST" if country_full=="EE"
replace country="FIN" if country_full=="FI"
replace country="FRA" if country_full=="FR"
replace country="GRC" if country_full=="EL"
replace country="HUN" if country_full=="HU"
replace country="ISL" if country_full=="IS"
replace country="IRL" if country_full=="IE"
replace country="ITA" if country_full=="IT"
replace country="LVA" if country_full=="LV"
replace country="LTU" if country_full=="LT"
replace country="LUX" if country_full=="LU"
replace country="MKD" if country_full=="MK"
replace country="MLT" if country_full=="MT"
replace country="NLD" if country_full=="NL"
replace country="NOR" if country_full=="NO"
replace country="POL" if country_full=="PL"
replace country="PRT" if country_full=="PT"
replace country="ROU" if country_full=="RO"
replace country="SVK" if country_full=="SK"
replace country="SVN" if country_full=="SI"
replace country="SCG" if country_full=="RS"
replace country="ESP" if country_full=="ES"
replace country="SWE" if country_full=="SE"
replace country="CHE" if country_full=="CH"
replace country="TUR" if country_full=="TR"
replace country="GBR" if country_full=="UK"
replace country="USA" if country_full=="US"
drop if country==""

rename country_full country_short
drop country_short 
order country year


drop if country=="USA"

save temp_eurostat_prices.dta, replace


********************************************************************************
***************************EUROSTAT, consumption********************************
********************************************************************************
import delimited "../international_data/original_data/consumption_gdp/eurostat_cons_categories.csv", clear

keep if unit=="CP_MNAC" /*in euros*/
rename time_period year
rename geo country 

drop dataflow freq unit unit obs_flag
rename obs_value value

sort country coicop year


keep if coicop=="TOTAL" | coicop=="CP09" | coicop=="CP091" | coicop=="CP092" | coicop=="CP093" | coicop=="CP094" | coicop=="CP095" | coicop=="CP096" | coicop=="CP05"


replace coicop="cons_total" if coicop=="TOTAL"
replace coicop="cons_rec_total" if coicop=="CP09"
replace coicop="cons_rec_audio_video" if coicop=="CP091"
replace coicop="cons_rec_books" if coicop=="CP095"
replace coicop="cons_rec_other_dur" if coicop=="CP092"
replace coicop="cons_rec_other_goods" if coicop=="CP093"
replace coicop="cons_rec_pack_hol" if coicop=="CP096"
replace coicop="cons_rec_services" if coicop=="CP094"
replace coicop="cons_hh_items" if coicop=="CP05"


reshape wide value, i(country year) j(coicop) string

local list total rec_total rec_audio_video rec_books rec_other_dur rec_other_goods rec_pack_hol rec_services hh_items
foreach var of local list {
	rename valuecons_`var' cons_`var'
}

label var cons_rec_audio_video "Consumption: Audio-visual, photographic and information (ES, current)"
label var cons_rec_books "Consumption: Newspapers and books  (ES, current)"
label var cons_rec_other_dur "Consumption: Other recreation durables (ES, current)"
label var cons_rec_other_goods "Consumption: Other recreation items and equipment (ES, current)"
label var cons_rec_pack_hol "Consumption: Package holidays (ES, current)"
label var cons_rec_services "Consumption: Recreation services (ES, current)"
label var cons_rec_total "Consumption: Total recreation and culture  (ES, current)"
label var cons_total "Consumption: Total consumption (ES, current)"


drop if cons_total==.

rename country country_full
gen country=""


replace country="AUT" if country_full=="AT"
replace country="BEL" if country_full=="BE"
replace country="BGR" if country_full=="BG"
replace country="HRV" if country_full=="HR"
replace country="CYP" if country_full=="CY"
replace country="CZE" if country_full=="CZ"
replace country="DEU" if country_full=="DE"
replace country="DNK" if country_full=="DK"
replace country="EST" if country_full=="EE"
replace country="FIN" if country_full=="FI"
replace country="FRA" if country_full=="FR"
replace country="GRC" if country_full=="EL"
replace country="HUN" if country_full=="HU"
replace country="ISL" if country_full=="IS"
replace country="IRL" if country_full=="IE"
replace country="ITA" if country_full=="IT"
replace country="LVA" if country_full=="LV"
replace country="LTU" if country_full=="LT"
replace country="LUX" if country_full=="LU"
replace country="MKD" if country_full=="MK"
replace country="MLT" if country_full=="MT"
replace country="NLD" if country_full=="NL"
replace country="NOR" if country_full=="NO"
replace country="POL" if country_full=="PL"
replace country="PRT" if country_full=="PT"
replace country="ROU" if country_full=="RO"
replace country="SVK" if country_full=="SK"
replace country="SVN" if country_full=="SI"
replace country="SCG" if country_full=="RS"
replace country="ESP" if country_full=="ES"
replace country="SWE" if country_full=="SE"
replace country="CHE" if country_full=="CH"
replace country="TUR" if country_full=="TR"
replace country="GBR" if country_full=="UK"
replace country="USA" if country_full=="US"
replace country="ALB" if country_full=="AL"
replace country="MNE" if country_full=="ME"
replace country="TUR" if country_full=="TR"
replace country="BIH" if country_full=="BA"

drop if country==""

rename country_full country_short
drop country_short 
order country year


drop if country=="USA"

merge 1:1 country year using temp_eurostat_prices
sort country year
drop _merge


keep if CPI_recreation_total!=.
gen CPI_rec_services_real=CPI_recreation_services/CPI_all
gen CPI_rec_real=CPI_recreation_total/CPI_all


gen aux_cons=cons_rec_audio_video+cons_rec_books+cons_rec_other_dur+cons_rec_other_goods+cons_rec_pack_hol
gen CPI_rec_nonserv_real=.
sort country year
local list audio_video books other_dur other_goods pack_hol 
gen gr=0
foreach var of local list{
	gen CPI_recreation_`var'_r=CPI_recreation_`var'/CPI_all
	by country: replace gr=gr+CPI_recreation_`var'_r/CPI_recreation_`var'_r[_n-1]*cons_rec_`var'/aux_cons
}

replace CPI_rec_nonserv_real=100 if gr==. & CPI_recreation_audio_video_r!=. & CPI_recreation_books_r!=. & CPI_recreation_other_dur_r!=. & CPI_recreation_other_goods_r!=. /*
	*/ & CPI_recreation_pack_hol_r!=. & aux_cons!=.

by country: replace CPI_rec_nonserv_real=CPI_rec_nonserv_real[_n-1]*gr if CPI_rec_nonserv_real==.
keep year country CPI_rec_services_real CPI_rec_real CPI_rec_nonserv_real

save TEMP_eurostat.dta, replace



********************************************************************************
********************************USA, prices*************************************
********************************************************************************
use "../US_prices_4_regions/prices_US_rec_detailed", replace
drop price_rec_sp_r
merge 1:1 year using "../CEX_data/rec_cex"


gen CPI_rec_services_real=price_oth_serv_r
gen CPI_rec_real=price_rec_weighted_r
gen aux_cons=rec_sport+rec_oth_goods+rec_photo+rec_audio_video+rec_pets+rec_reading
gen CPI_rec_nonserv_real=.
sort year

local list pets sport reading oth_goods photo audio_video
gen gr=0
foreach var of local list{
	replace gr=gr+price_`var'_r/price_`var'_r[_n-1]*rec_`var'/aux_con
}
replace CPI_rec_nonserv_real=100 if year==1980
replace CPI_rec_nonserv_real=CPI_rec_nonserv_real[_n-1]*gr if CPI_rec_nonserv_real==.

gen country="USA"
keep year country CPI_rec_services_real CPI_rec_real CPI_rec_nonserv_real

local list rec_services_real rec_real rec_nonserv_real
gen byte tag=1 if year==2015
sort tag
foreach var of local list {
	gen CPI_`var'_aux=100*CPI_`var'/CPI_`var'[1]
	drop CPI_`var'
	rename CPI_`var'_aux CPI_`var'
}
drop tag
sort year
save TEMP_USA.dta, replace


****** plots
use TEMP_USA.dta, replace
append using TEMP_eurostat.dta

local list rec_services_real rec_real rec_nonserv_real
gen byte tag=1 if year==2010
sort country tag
foreach var of local list {
	by country: gen CPI_`var'_aux=CPI_`var'/CPI_`var'[1]
	drop CPI_`var'
	rename CPI_`var'_aux CPI_`var'
}
drop tag
sort country year


*trends
gen l_CPI_rec_services_real=log(CPI_rec_services_real)
gen l_CPI_rec_real=log(CPI_rec_real)
gen l_CPI_rec_nonserv_real=log(CPI_rec_nonserv_real)
reghdfe CPI_rec_services_real year, absorb(country) cluster(country year)
reghdfe CPI_rec_real year, absorb(country) cluster(country year)
reghdfe CPI_rec_nonserv_real year, absorb(country) cluster(country year)


local flag_trends=1
if `flag_trends'==1 {

			save data_init, replace
			
			sort country year


			***country-by-country trends
			preserve
				statsby n=e(N) _b _se e(df_r), by(country) clear: regress l_CPI_rec_services year, vce(robust)
				rename _b_year gamma_rec_services
				keep gamma_rec_services country
				save gamma_rec_services, replace
			restore 
			preserve
				statsby n=e(N) _b _se e(df_r), by(country) clear: regress l_CPI_rec_real year, vce(robust)
				rename _b_year gamma_rec_total
				keep gamma_rec_total country
				save gamma_rec_total, replace
			restore 			
			preserve
				statsby n=e(N) _b _se e(df_r), by(country) clear: regress l_CPI_rec_nonserv_real year, vce(robust)
				rename _b_year gamma_rec_nonserv
				keep gamma_rec_nonserv country
				save gamma_rec_nonserv, replace
			restore 				

			use gamma_rec_services, replace
			merge 1:1 country using gamma_rec_total
			drop _merge
			merge 1:1 country using gamma_rec_nonserv
			drop _merge
			save gamma_merged, replace
			erase gamma_rec_services.dta
			erase gamma_rec_total.dta
			erase gamma_rec_nonserv.dta
			
			use data_init.dta, clear

}




*graphs
separate CPI_rec_services_real, by(country) veryshortlabel 
twoway (line `r(varlist)' year), name("Services")/*
*/ylabel(0.5[0.25]2)  xtitle("") ytitle("Price index of recreational services, 2010=1") legend(off)/*
*/bgcolor(white) graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) xlabel(,grid nogextend gmax) ylabel(,grid nogextend gmin gmax) ysize(4)
if `save_figures_flag'==1{
	graph export "../figures/recreation_prices_services.png", as(png) replace
	graph export "../figures/recreation_prices_services.eps", as(eps) replace
}

separate CPI_rec_nonserv_real, by(country) veryshortlabel 
twoway (line `r(varlist)' year), name("Nonservices")/*
*/ylabel(0.5[0.25]2)  xtitle("") ytitle("Price index of recreational goods, 2010=1") legend(off)/*
*/bgcolor(white) graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) xlabel(,grid nogextend gmax) ylabel(,grid nogextend gmin gmax) ysize(4)
if `save_figures_flag'==1{
	graph export "../figures/recreation_prices_nonservices.png", as(png) replace
	graph export "../figures/recreation_prices_nonservices.eps", as(eps) replace
}


*black and white
gen CPI_rec_services_real_bw=CPI_rec_services_real
separate CPI_rec_services_real_bw, by(country) veryshortlabel 
twoway (line `r(varlist)' year), name("ServicesBW")/*
*/ylabel(0.5[0.25]2)  xtitle("") ytitle("Price index of recreational services, 2010=1") legend(off)/*
*/bgcolor(white) graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) xlabel(,grid nogextend gmax) ylabel(,grid nogextend gmin gmax) ysize(4) scheme(s1mono)
if `save_figures_flag'==1{
	graph export "../figures/recreation_prices_services_bw.png", as(png) replace
	graph export "../figures/recreation_prices_services_bw.eps", as(eps) replace
}

gen CPI_rec_nonserv_real_bw=CPI_rec_nonserv_real
separate CPI_rec_nonserv_real_bw, by(country) veryshortlabel 
twoway (line `r(varlist)' year), name("NonservicesBW")/*
*/ylabel(0.5[0.25]2)  xtitle("") ytitle("Price index of recreational goods, 2010=1") legend(off)/*
*/bgcolor(white) graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) xlabel(,grid nogextend gmax) ylabel(,grid nogextend gmin gmax) ysize(4) scheme(s1mono)
if `save_figures_flag'==1{
	graph export "../figures/recreation_prices_nonservices_bw.png", as(png) replace
	graph export "../figures/recreation_prices_nonservices_bw.eps", as(eps) replace
}




erase data_init.dta
erase TEMP_eurostat.dta
erase TEMP_USA.dta
erase temp_eurostat_prices.dta
erase gamma_merged.dta

